package com.cl.edu.web.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.common.util.StringUtil;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Area;
import com.cl.edu.entity.Country;
import com.cl.edu.entity.Education;
import com.cl.edu.entity.Highschool;
import com.cl.edu.entity.Message;
import com.cl.edu.entity.Status;
import com.cl.edu.entity.Subject;
import com.cl.edu.entity.Teacher;
import com.cl.edu.entityVO.Photo;
import com.cl.edu.service.RegisterManager;
import com.cl.edu.service.admin.TeachersManager;

/**
 * 教员信息管理
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class TeacherAction extends CrudActionSupport<Teacher>{

	@Autowired
	private TeachersManager teacherManager;
	@Autowired
	private RegisterManager registerManager;
	
	private Teacher entity;	 
	private Long id;
	
	private Page<Teacher> page = new Page<Teacher>();
	
	private String filter_id;
	private String filter_realname;
	private String filter_jianliFlag;
	private String filter_step;
	
	private List<Photo> photosList = new ArrayList<Photo>();		//照片列表
	private String savePath = File.separator+"upload"+File.separator+"teachers";
	private List<Country> countryList;//国家列表
	private List<Area> shengList; //省列表
	private List<Highschool> hxList;//高校列表
	private List<Area>  syList;//沈阳区划
	private List<Education> eduList;//学历列表
	private List<Status> statusList1;//身份列表1
	private List<Status> statusList2;//身份列表2
	private List<Status> statusList3;//身份列表3
	private List<Subject> subjectList;//教师任教学科列表
	/**
	 * 查询所有通过审核的教员列表
	 */
	@Override
	public String list() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = teacherManager.getTeachers(page,0);
		return SUCCESS;
	}
	
	/**
	 * 查询所有未通过审核的教员列表
	 * @return
	 * @throws Exception
	 */
	public String uncheck() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = teacherManager.getTeachers(page,1);
		return "uncheck";
	}
	
	//查询符合每周之星条件的教员(教员通过审核并且有照片)
	public String weekstar() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = teacherManager.getWeekstarTeachers(page);
		return "weekstar";
	}
	
	/**
	 * 通过教员审核
	 * @return
	 * @throws Exception
	 */
	public String check() throws Exception {
		Teacher teacher = teacherManager.getTeacher(id);
		teacher.setState(0);
		teacher.setPasstime(new Date());
		teacherManager.updateTeacher(teacher); //审核通过
		
		Message message = new Message();
		message.setTo(id.intValue());
		message.setMsg("您已通过审核，欢迎来到成龙网!");
		message.setIsRead(0);
		message.setCreateTime(new Date());
		teacherManager.saveMessage(message);
		
		addActionMessage("操作成功!");
		return uncheck();
	}
	
	/**
	 * 修改教员星级
	 * @return
	 * @throws Exception
	 */
	public String change() throws Exception {
		Teacher teacher = teacherManager.getTeacher(id);
		String step = Struts2Utils.getParameter("step");
		teacher.setStep(Integer.parseInt(step));
		teacherManager.updateTeacher(teacher); //星级修改
		
		Message message = new Message();
		message.setTo(id.intValue());
		message.setMsg("您的星级已被管理员修改为"+step+"级!");
		message.setIsRead(0);
		message.setCreateTime(new Date());
		teacherManager.saveMessage(message);
		
		addActionMessage("星级修改成功!");
		return list();
	}
	
	/**
	 * 修改教员被介绍次数
	 * @return
	 * @throws Exception
	 */
	public String changenum() throws Exception {
		Teacher teacher = teacherManager.getTeacher(id);
		String introduceNum = Struts2Utils.getParameter("introduceNum");
		teacher.setIntroduceNum(introduceNum);
		teacherManager.updateTeacher(teacher); //被介绍次数修改
		
		addActionMessage("次数修改成功!");
		return list();
	}
	
	/**
	 * 修改教员成为每周之星次数
	 * @return
	 * @throws Exception
	 */
	public String starnum() throws Exception {
		Teacher teacher = teacherManager.getTeacher(id);
		String starNum = Struts2Utils.getParameter("starNum");
		teacher.setStarNum(starNum);
		teacherManager.updateTeacher(teacher); //成为每周之星次数
		
		addActionMessage("次数修改成功!");
		return weekstar();
	}
	
	/**
	 * 修改教员成为每周之星展示
	 * @return
	 * @throws Exception
	 */
	public String isweekstar() throws Exception{
		String isWeekStar = Struts2Utils.getParameter("isWeekStar");
		Integer star = Integer.parseInt(isWeekStar);
		Teacher teacher = teacherManager.getTeacherByStar(star);
		if(teacher!=null){
			teacher.setIsWeekStar(0);
			teacherManager.updateTeacher(teacher);
		}
		//更新完已存在的每周之星，再设置新的
		teacher = teacherManager.getTeacher(id);
		teacher.setIsWeekStar(Integer.parseInt(isWeekStar));
		teacherManager.updateTeacher(teacher); //修改教员成为每周之星展示
		addActionMessage("每周之星设置修改成功!");
		return weekstar();
	}
	
	/**
	 * 删除一周之星
	 * @return
	 * @throws Exception
	 */
	public String deletestar() throws Exception {
		Teacher teacher = teacherManager.getTeacher(id);
		teacher.setIsWeekStar(0);
		teacherManager.updateTeacher(teacher);
		addActionMessage("每周之星删除成功!");
		return weekstar();
	}
	
	/**
	 * 查询教员详细的注册信息
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		
		countryList	  = registerManager.getCountries();
		shengList     = registerManager.getSheng();
		hxList        = registerManager.getHighschools();
		syList        = registerManager.getShenYang();
		eduList       = registerManager.geteEducations();
		statusList1   = registerManager.getStatus(1L);
		statusList2   = registerManager.getStatus(2L);
		statusList3   = registerManager.getStatus(3L);
		subjectList   = registerManager.getSubjects("0");
		return "detail";
	}
	
	/**
	 * 查询教员照片信息
	 * @return
	 * @throws Exception
	 */
	public String photo() throws Exception {
		entity = teacherManager.getTeacher(id);
		String photos = entity.getPhotos();
		if(StringUtil.isNotEmpty(photos)){
			String[] urls = photos.split("@@");
			for(String url:urls){
				String[] temp = url.split("@");
				Photo photo = new Photo();
				photo.setName(temp[0]);
				photo.setDesc(temp[1]);
				photosList.add(photo);
			}
		}
		return "photo";
	}
	
	/**
	 * 删除图片
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		entity = teacherManager.getTeacher(id);
		String name = Struts2Utils.getParameter("name");
		File file = new File(getSavePath() + File.separator+id+File.separator+name);
		if(file.exists()){ //如果存在则删除文件
			file.delete();
		}
		String photo   = entity.getPhoto();
		String weekStarPhoto = entity.getWeekStarPhoto();
		if(name.equals(photo)){ //如删除的是头像，则头像为空
			entity.setPhoto(null);
		}
		if(name.equals(weekStarPhoto)){//如与一周之星图像设置相同，则清空一周之星图像
			entity.setWeekStarPhoto(null);
			entity.setIsWeekStar(0);//清除一周之星的显示
		}
		String  photos = entity.getPhotos();
		entity.setPhotos(photos.replaceAll(name+"@.*?@@", ""));
		if(StringUtil.isEmpty(entity.getPhotos())){
			entity.setPhotos(null);
		}
		teacherManager.updateTeacher(entity);
		addActionMessage("删除成功!");
		return  photo();
	}
	
	
	/**
	 * 待设置为一周之星显示用的照片信息
	 * @return
	 * @throws Exception
	 */
	public String weekstarphoto() throws Exception {
		entity = teacherManager.getTeacher(id);
		String photos = entity.getPhotos();
		if(StringUtil.isNotEmpty(photos)){
			String[] urls = photos.split("@@");
			for(String url:urls){
				String[] temp = url.split("@");
				Photo photo = new Photo();
				photo.setName(temp[0]);
				photo.setDesc(temp[1]);
				photosList.add(photo);
			}
		}
		return "weekstarphoto";
	}
	 
	/**
	 * 设置一周之星显示图片
	 * @return
	 * @throws Exception
	 */
	public String setphoto() throws Exception {
		entity = teacherManager.getTeacher(id);
		String name = Struts2Utils.getParameter("name");
		entity.setWeekStarPhoto(name);
		teacherManager.updateTeacher(entity);
		addActionMessage("设置成功!");
		return weekstarphoto();
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		try {
			teacherManager.updateTeacher(entity);
			addActionMessage("修改教员信息成功!");
		} catch (Exception e) {
			addActionMessage("修改教员信息失败!");
			e.printStackTrace();
		}
		return detail();
	}

	/**
	 * 删除教员信息
	 */
	@Override
	public String delete() throws Exception {
		teacherManager.delBookingstudents(id);//删除有关联的预约信息
		teacherManager.delBookingteachers(id);
		teacherManager.delMessage(id);
		teacherManager.delTs(id);
		teacherManager.delTeacher(id);
		addActionMessage("删除教员信息成功!");
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id == null){
			entity = new Teacher();
		}else {
			entity = teacherManager.getTeacher(id);
		}
	}
	
	/**
	 * 在detail()前执行二次绑定.
	 */
	public void prepareDetail() throws Exception
	{
		prepareModel();
	}
	
	public Teacher getModel() {
		return entity;
	}


	public Teacher getEntity() {
		return entity;
	}

	public void setEntity(Teacher entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<Teacher> getPage() {
		return page;
	}

	public void setPage(Page<Teacher> page) {
		this.page = page;
	}

	public String getFilter_realname() {
		return filter_realname;
	}

	public void setFilter_realname(String filter_realname) {
		this.filter_realname = filter_realname;
	}

	public List<Photo> getPhotosList() {
		return photosList;
	}

	public void setPhotosList(List<Photo> photosList) {
		this.photosList = photosList;
	}

	public String getFilter_id() {
		return filter_id;
	}

	public void setFilter_id(String filterId) {
		filter_id = filterId;
	}

	public String getFilter_jianliFlag() {
		return filter_jianliFlag;
	}

	public void setFilter_jianliFlag(String filterJianliFlag) {
		filter_jianliFlag = filterJianliFlag;
	}

	public String getFilter_step() {
		return filter_step;
	}

	public void setFilter_step(String filterStep) {
		filter_step = filterStep;
	}
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public List<Country> getCountryList() {
		return countryList;
	}

	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}

	public List<Area> getShengList() {
		return shengList;
	}

	public void setShengList(List<Area> shengList) {
		this.shengList = shengList;
	}

	public List<Highschool> getHxList() {
		return hxList;
	}

	public void setHxList(List<Highschool> hxList) {
		this.hxList = hxList;
	}

	public List<Area> getSyList() {
		return syList;
	}

	public void setSyList(List<Area> syList) {
		this.syList = syList;
	}

	public List<Education> getEduList() {
		return eduList;
	}

	public void setEduList(List<Education> eduList) {
		this.eduList = eduList;
	}

	public List<Status> getStatusList1() {
		return statusList1;
	}

	public void setStatusList1(List<Status> statusList1) {
		this.statusList1 = statusList1;
	}

	public List<Status> getStatusList2() {
		return statusList2;
	}

	public void setStatusList2(List<Status> statusList2) {
		this.statusList2 = statusList2;
	}

	public List<Status> getStatusList3() {
		return statusList3;
	}

	public void setStatusList3(List<Status> statusList3) {
		this.statusList3 = statusList3;
	}

	public List<Subject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}

}
